package com.db.collections.hashmapint;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.TreeMap;

/**
 * @author nulls.narik
 */
public class TestHashMapInt {

    public static void main(String[] args) {
        HashMapInt map = new HashMapInt();

        map.put(1, 3);
        System.out.println("get: " + map.get(1));
        System.out.println("containsKey: " + map.containsKey(1));
        map.remove(1);
        System.out.println("containsKey: " + map.containsKey(1));
        map.put(2, 5);
        System.out.println("get: " + map.get(2));
        map.put(2, 6);
        System.out.println("replace: " + map.get(2));
        try {
            map.get(1);
            System.out.println("error");
        } catch (HashMapInt.NotFoundKeyException e) {
            System.out.println("not found key: " + e.getMessage());
        }

        int n = 100000;

        long memory = 0, memoryArray = 0, memoryHashMap = 0, memoryHashMapInt = 0;
        int[] array;
        HashMap<Integer, Integer> hashMap;
        HashMapInt hashMapInt;

        System.gc();
        try {
            Thread.sleep(1000);
        } catch (InterruptedException e) {
        }
        memory = Runtime.getRuntime().totalMemory() - Runtime.getRuntime().freeMemory();
        array = new int[n];
        for (int i = 0; i < n; i++) {
            array[i] = i;
        }
        memoryArray = Runtime.getRuntime().totalMemory() - Runtime.getRuntime().freeMemory() - memory;

        System.gc();
        try {
            Thread.sleep(1000);
        } catch (InterruptedException e) {
        }
        memory = Runtime.getRuntime().totalMemory() - Runtime.getRuntime().freeMemory();
        hashMap = new HashMap<Integer, Integer>();
        for (int i = 0; i < n; i++) {
            hashMap.put(i, i);
        }
        memoryHashMap = Runtime.getRuntime().totalMemory() - Runtime.getRuntime().freeMemory() - memory;

        System.gc();
        try {
            Thread.sleep(1000);
        } catch (InterruptedException e) {
        }
        memory = Runtime.getRuntime().totalMemory() - Runtime.getRuntime().freeMemory();
        hashMapInt = new HashMapInt();
        for (int i = 0; i < n; i++) {
            hashMapInt.put(i, i);
        }
        memoryHashMapInt = Runtime.getRuntime().totalMemory() - Runtime.getRuntime().freeMemory() - memory;


        int sum = 0;
        for (int i = 0; i < n; i++) {
            sum += array[i];
        }
        sum = sum / n;
        System.out.println(String.format("Avr: %d", sum));

        sum = 0;
        for (int i = 0; i < n; i++) {
            sum += hashMap.get(i);
        }
        sum = sum / n;
        System.out.println(String.format("Avr: %d", sum));

        sum = 0;
        for (int i = 0; i < n; i++) {
            sum += hashMapInt.get(i);
        }
        sum = sum / n;
        System.out.println(String.format("Avr: %d", sum));

        System.out.println(String.format("Memory array: %d {%.2f}; memory HashMap: %d [%.2f] {%.2f};" +
                " memory HashMapInt: %d [%.2f] {%.2f};%nbest: %.2f",
                memoryArray, (float) memoryArray / n,
                memoryHashMap, (float) memoryHashMap / memoryArray, (float) memoryHashMap / n,
                memoryHashMapInt, (float) memoryHashMapInt / memoryArray, (float) memoryHashMapInt / n,
                (float) memoryHashMap / memoryHashMapInt));
    }

}
